package com.xy.modular.basics.action;

import com.alibaba.fastjson.JSONArray;
import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.modular.basics.entity.user.UserAuthLocal;
import com.xy.modular.basics.service.IUserService;
import com.xy.shiro.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(description = "登录相关接口")
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

	static Logger log = LoggerFactory.getLogger(LoginController.class);

	@Resource
	private IUserService userService;

	@ApiOperation(value = "登录", notes = "")
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody MReturnObject login(UserAuthLocal userAuthLocal, Model model, String username,
			HttpServletRequest request) {
		try {
			Subject subject = SecurityUtils.getSubject();
			PageRequest pageRequest = newPageRequest(request);
			System.out.println(username + "    " + userAuthLocal.getPassword());
			UsernamePasswordToken token = new UsernamePasswordToken(username,
					userAuthLocal.getPassword().toCharArray());
			token.setRememberMe(true);
			subject.login(token);
			// return new MReturnObject(MReturnObject.SUCCESS,
			// JWT.sign(userAuths, 30L * 24L * 3600L * 1000L));

			// return new MReturnObject("20000", "{'token':"+JWT.sign(userAuths,
			// 30L * 24L * 3600L * 1000L)+"}");

			// Role role = rolesMapper.getRole(username);
			String token1 = TokenUtil.getToken(username, "username", request.getRemoteAddr());

			return new MReturnObject(20000, token1);
			// return new MReturnObject(20000,
			// JSONArray.parse("{'token':'admin-token'}"));
			// {"code":20000,"data":{"token":"admin-token"}}
		} catch (Exception e) {
			return new MReturnObject(MReturnObject.LOGIN_ERROR, MReturnUntil.ERROR_LOGIN);
		}
	}

	@RequestMapping(value = "/error", method = RequestMethod.GET)
	public @ResponseBody MReturnObject loginError(Model model) {
		return new MReturnObject(MReturnObject.ERROR, MReturnUntil.WARNING_NOT_LOGIN);
	}

	@RequestMapping(value = "/verification", method = RequestMethod.GET)
	public @ResponseBody MReturnObject verification(Model model) {
		// 因为我们用过 Shiro 用户认证后是存放在 Shiro 的 session 中.
		Subject subject = SecurityUtils.getSubject();
		// 取出身份信息
		return new MReturnObject(20000, subject.getPrincipal());
		/*return new MReturnObject(20000, JSONArray.parse(
				"{roles: ['admin'],introduction: 'I am a super administrator',avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',name: 'Super Admin'}"));*/
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public @ResponseBody MReturnObject timeOut(Model model) {
		return new MReturnObject(MReturnObject.SUCCESS, "success");
	}
}
